package org.dant.ant.extension.net.repository;

import java.util.*;
import org.dant.ant.extension.utils.StringUtils;

public class ProcessGroup {

    protected String sourceHost;
  
    protected String commandLine;
    protected boolean blocking;

    // From Ant perspective
    protected String projectName;
    protected String targetName;
    protected String taskName;
  
    // From Dant perspective
    protected String testSuiteName;
    protected String testCaseName;
  
    protected LinkedList processItems = new LinkedList();

    public int getCount() {
        return this.processItems.size();
    }

    public void add(ProcessItem pi) {
        processItems.add(pi);
        pi.setOwningGroup(this);
    }

    public void remove(ProcessItem pi) {
        pi.destroy();
        processItems.remove(pi);
    }

    public ProcessItem getLast() {
        return (ProcessItem)processItems.getLast();
    }

    public void removeLast() {
        remove((ProcessItem)processItems.getLast());
    }

    public void removeProcess(Process process) {
        for (int i = 0; i < processItems.size(); i++) {
            ProcessItem pi = (ProcessItem) processItems.get(i);
            if (process.equals(pi.getProcess())) {
                remove(pi);
                break;
            }
        }
    }

    public void removeAll() {
        int count = getCount();
        for (int i = 0; i < count; i++) {
            removeLast();
        }
    }

    public void setCommandLine(String cmdline) {
        this.commandLine = cmdline;
    }

    public String getCommandLine() {
        return this.commandLine;
    }

    public void setProjectName(String prjname) {
        this.projectName = prjname;
    }

    public String getProjectName() {
        return this.projectName;
    }

    public void setTargetName(String t) {
        this.targetName = t;
    }

    public String getTargetName() {
        return this.targetName;
    }

    public void setTaskName(String t) {
        this.taskName = t;
    }

    public String getTaskName() {
        return this.taskName;
    }

    public void setTestSuiteName(String t) {
        this.testSuiteName = t;
    }

    public String getTestSuiteName() {
        return this.testSuiteName;
    }

    public void setTestCaseName(String t) {
        this.testCaseName = t;
    }

    public String getTestCaseName() {
        return this.testCaseName;
    }

    public void setSourceHost(String s) {
        this.sourceHost = s;
    }

    public String getSourceHost() {
        return this.sourceHost;
    }

    public void setBlocking(boolean b) {
        this.blocking = b;
    }

    public boolean getBlocking() {
        return this.blocking;
    }

    public String export() {
        StringBuffer sb = new StringBuffer();
    
        sb.append(StringUtils.format("Source host:", 4));
        sb.append(StringUtils.formatln(sourceHost));
        sb.append(StringUtils.format("Project name: ", 4));
        sb.append(StringUtils.formatln(projectName));
        sb.append(StringUtils.format("Target name: ", 4));
        sb.append(StringUtils.formatln(targetName));
        sb.append(StringUtils.format("Task name: ", 4));
        sb.append(StringUtils.formatln(taskName));
        sb.append(StringUtils.format("Test suite: ", 4));
        sb.append(StringUtils.formatln(testSuiteName));
        sb.append(StringUtils.format("Test case: ", 4));
        sb.append(StringUtils.formatln(testCaseName));
        sb.append(StringUtils.format("Command line: ", 4));
        sb.append(StringUtils.formatln(commandLine));
        sb.append(StringUtils.format("Blocking: ", 4));
        sb.append(StringUtils.formatln(blocking + ""));
    
        for (Iterator iter = processItems.iterator(); iter.hasNext();) {
            ProcessItem pi = (ProcessItem) iter.next();
            sb.append(pi.export());
        }

        return sb.toString();
    }
}
